package com.happyscan138.makefun.Fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.happyscan138.makefun.BaseAdapter.systemNews_listview;
import com.happyscan138.makefun.MypersionalNews;
import com.happyscan138.makefun.R;
import com.happyscan138.makefun.Urls.Urls;
import com.happyscan138.makefun.adapter.systemNewsAdapter;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestHandle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

import static android.content.Context.MODE_PRIVATE;
import static com.happyscan138.makefun.base.http.client;
import static com.happyscan138.makefun.base.http.params;
import static com.happyscan138.makefun.base.share.access_key;
import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2016/11/22.
 */

public class news_Fragment extends android.support.v4.app.Fragment implements AdapterView.OnItemClickListener{
    private ListView listViewNews;
    private List<systemNews_listview> listdata;
    private SharedPreferences preferences;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.newsfragment,container,false);
        preferences = getActivity().getSharedPreferences("UserInfo", MODE_PRIVATE);
        access_key=preferences.getString("UserId","");
        initView(view);
        getAdapterView();
        return view;
    }
    public void initView(View view){
        listViewNews=(ListView)view.findViewById(R.id.main_newfragment_listview);
        listViewNews.setOnItemClickListener(this);
    }
    public View getAdapterView(){
        params.put("access_key",access_key);
        params.put("last_msg_id",0);
        params.put("page_count",10);
        View view=null;
        listdata=new ArrayList<>();
        final systemNews_listview data=new  systemNews_listview();
        log.e("paras",params.toString());
        RequestHandle handle=client.post(Urls.SystemNews,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                log.e("onSuccess",response.toString());
                try {
                    JSONObject jsonObject=new JSONObject(response.toString());
                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject obj=jsonArray.getJSONObject(i);

                        data.text=obj.getString("msg");
                        log.e("data",data.toString());
                        listdata.add(data);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }
        });
        listViewNews.setAdapter(new systemNewsAdapter(getActivity(),listdata));

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(new Intent(getActivity(), MypersionalNews.class));
    }
}
